package com.hfy.music.mapper;

import com.hfy.model.vo.CanAddSheetVo;
import com.hfy.model.vo.MusicVo;
import com.hfy.model.vo.SheetVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @ author 韩
 * time: 2024/5/29 18:53
 */

@Mapper
public interface SheetMusicMapper {

    @Insert("INSERT INTO `music_sheet` (`account`,`name`) VALUES (#{account},#{sheetName})")
    void createSheet(@Param("account") Long account,@Param("sheetName") String sheetName);

    @Update("UPDATE `music_sheet` SET `name` = #{sheetName}")
    void updateSheet(@Param("sheetName") String sheetName);

    @Delete("DELETE FROM `music_sheet` WHERE id = #{sheetId}")
    void deleteSheet(@Param("sheetId") Integer sheetId);

    @Insert("INSERT INTO `sheet_music` (`sheet_id`,`music_id`) VALUES (#{sheetId},#{musicId})")
    void addMusicToSheet(@Param("sheetId") Integer sheetId,@Param("musicId") Integer musicId);

    @Delete("DELETE FROM `sheet_music` WHERE `sheet_id` = #{sheetId} AND `music_id` = #{musicId}")
    void deleteMusicToSheet(@Param("sheetId") Integer sheetId,@Param("musicId") Integer musicId);

//    @Select("SELECT `id`, `account`,`name`, (count(*)) AS `musicNum` FROM `music_sheet` WHERE `account` = #{account}")
    @Select("SELECT `id` ,`account`,`name`, (SELECT COUNT(*) FROM sheet_music WHERE sheet_id = music_sheet.id) AS `musicNum` FROM `music_sheet` WHERE `account` = #{account}")
    List<SheetVo> showSheet(@Param("account") Long account);

    @Select("SELECT `id`,`name` FROM `music_sheet` WHERE `account` = #{account}")
    List<CanAddSheetVo> showCanAddSheet(@Param("account") Long account);

    @Select("SELECT * FROM `sheet_music` WHERE  sheet_id=#{sheetId} AND music_id=#{musicId}")
    Integer queryHaveFormSheet(@Param("sheetId") Integer sheetId, @Param("musicId") Integer musicId);

    @Select("SELECT `id`,`name`, `music`, `cover`, `musician`,`createTime`,`albumId`,`duration` FROM" +
            " `music` WHERE id IN" +
            " (SELECT music_id FROM `sheet_music` WHERE `id` = #{sheetId})")
    List<MusicVo> getMusicList(@Param("sheetId") String sheetId);

}
